package org.kassa.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.kassa.model.Item;
import org.kassa.service.Logger;

public class ItemDaoImpl implements ItemDao {
        
    
        //Getters
        @Override
	public String getItemNameById(int id) {
            try {
                String sql = "SELECT name FROM items WHERE id = ?";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setInt(1, id);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) return rs.getString("name");
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            
            return "0";
	}
	
        @Override
	public int getItemIdByCode(int code) {
            try {
		String sql = "SELECT id FROM items WHERE code = ?";
		PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
		ps.setInt(1, code);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			return rs.getInt("id");
		}
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
		return 0;
	}
        
        @Override
        public double getItemPriceByCode(int code) {
            try {
                String sql = "SELECT price FROM items WHERE code = ?";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setInt(1, code);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) return rs.getDouble("price");
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            return 0F;
        }
	
        @Override
	public int getItemsQuantityById(int id) {
            try {
                String sql = "SELECT quantity FROM items WHERE id = ?";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setInt(1, id);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                        return rs.getInt("quantity");
                }

            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            return 0;
        }
        
        //Adders
        @Override
        public boolean addItemToList(Item item) {
            try {
                String sql = "INSERT INTO items (name, code, price) VALUES (?, ?, ?)";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setString(1, item.getName());
                ps.setInt(2, item.getCode());
                ps.setDouble(3, item.getPrice());
                if (ps.executeUpdate() == 1) return true;
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            return false;
        }	
        
        @Override
        public boolean updateItemPrice(Item item) {
            try {
                String sql = "UPDATE items SET price = ? WHERE id = ?";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setDouble(1, item.getPrice());
                ps.setInt(2, item.getId());
                if (ps.executeUpdate() == 1) return true;
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            return false;
        }
        
        
        //Checkers
        @Override
        public boolean isThereSomeCode(int code) {
            try {
                String sql = "SELECT id FROM items WHERE code = ?";
                PreparedStatement ps = DaoHelperImpl.getPreparedStatement(sql);
                ps.setInt(1, code);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {return true;}
            } catch (SQLException e) {
                Logger.log("SQLException caught");
                e.printStackTrace();
            }
            return false;
        }        
}
